package com.example.haiyang.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.haiyang.common.GetTrueIp;
import com.example.haiyang.common.utils.MD5Utils;
import com.example.haiyang.domain.User;
import com.example.haiyang.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class LoginController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    IUserService iUserService;

    @Autowired
    private GetTrueIp getTrueIp;

    @RequestMapping("/register")
    public String register(User user, String vcode, HttpServletRequest request){
        try {
            System.out.println(user.toString());
            user.setId(UUID.randomUUID().toString());
            //用户名不能重复
            User name = iUserService.selectOne(new EntityWrapper<User>().eq("name", user.getName()));
            if (name!=null){
                //iUserService.insert(user);
                return "common/false";
            }
            user.setPassword(MD5Utils.getEncryptedPwd(user.getPassword()));
            iUserService.insert(user);
            System.out.println(vcode);
        }catch (Exception e){
            logger.info("在ip [{}] 时发生异常",getTrueIp.getIpAddr(request));
            e.printStackTrace();
        }
        return "login/login";
    }



    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request,Model model){
        User selectUser =null;
        try {
            selectUser = iUserService.selectOne(new EntityWrapper<User>().eq("name", user.getName()));
        }catch (Exception e){
            logger.info("没有输入用户名");
        }

        if (selectUser==null){
            model.addAttribute("success","false");
            model.addAttribute("content","用户名不存在");
            /*returnMap(false,"用户名不存在");*/
            return "common/success";
        }
        try {
            boolean b = MD5Utils.validPassword(user.getPassword(), selectUser.getPassword());
            if (!b){
                logger.info("密码错误 ip：[{}],用户名：[{}]",getTrueIp.getIpAddr(request),user.getName());
                return "/common/500";
            }
        } catch (Exception e) {
            logger.info("密码错误 ip：[{}],用户名：[{}]",getTrueIp.getIpAddr(request),user.getName());
            e.printStackTrace();
        }
        model.addAttribute("success","true");
        model.addAttribute("content","登录成功");
        /*returnMap(true,"成功");*/
        return "common/success";
    }


    public static Map returnMap(Boolean bool,String context){
        Map map = new HashMap<>();
        map.put("success",bool);
        map.put("data",context);
        return map;
    }

    @RequestMapping("/getward")
    public String getward(){
        return "/login/getPassword";
    }
}
